<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/16 12:04
 */

namespace App\Project\Printer;

use App\Models\MedicalComplaint;
use Carbon\Carbon;

class ComplaintPrinter extends PrinterAbstract
{
    public function __construct(MedicalComplaint $complaint)
    {
        $this->model = $complaint;
    }

    public function symptoms()
    {
        $symptom = $this->model->symptoms;

        if (!$symptom) {
            return '';
        }

        $str = '';
        $now = Carbon::now();

        foreach ($symptom as $key => $item) {

            $print = [
                $this->fei('symptoms', $key.'.name', '症状：'),
            ];

            if (array_key_exists('started_at', $item)) {
                $started_at = Carbon::parse($item['started_at']);

                if (($diff = $started_at->diffInMonths($now)) < 12) {
                    if ($diff) {
                        $print[] = '发病' . $started_at->diffInMonths() . '个月';
                    } else {
                        $print[] = '当月发病';
                    }
                } else {
                    $print[] = $this->fe('symptoms', $key.'.started_at', '发病时间：');
                }
            }

            if (array_key_exists('exacerbated_at', $item)) {
                $exacerbated_at = Carbon::parse($item['exacerbated_at']);

                if (($diff = $exacerbated_at->diffInMonths($now)) < 12) {
                    if ($diff) {
                        $print[] = '加重' . $exacerbated_at->diffInMonths() . '个月';
                    } else {
                        $print[] = '当月加重';
                    }
                } else {
                    $print[] = $this->fe('symptoms', $key.'.exacerbated_at', '加重时间');
                }
            }

            $str .= $this->getPrintStr('', $print);
        }

        return $str;
    }

    public function weightchange()
    {

        $change = $this->model->weightchange;

        if (!$change || ($change['key'] == 0)) {
            return '体重无变化；';
        }

        $print = [
            $change['value'] . $this->model->weightchangestatus['value'],
        ];

        if ($this->model->weightchangestatus['key'] == 1) {
            $print[] = $this->fe('weightchangekg', null, '变化值：', 'KG');
        }

        return $this->getPrintStr('体重变化：', $print);
    }

    public function bloodGlucose()
    {
        $blood = $this->model->blood_glucose;

        if (!$blood) {
            return '';
        }

        $print = [];

        if (isset($blood['limosis']['low'])) {
            $print[] = $this->fe('blood_glucose', 'limosis.high', '空腹血糖：'.$this->fe('blood_glucose', 'limosis.low').'-', 'mmol/L');
        } else {
            $print[] = $this->fe('blood_glucose', 'limosis.high', '空腹血糖： -', 'mmol/L');
        }

        if (isset($blood['postprandial']['low'])) {
            $print[] = $this->fe('blood_glucose', 'postprandial.high', '餐后2h血糖：'.$this->fe('blood_glucose', 'postprandial.low').'-', 'mmol/L');
        } else {
            $print[] = $this->fe('blood_glucose', 'postprandial.high', '餐后2h血糖： -', 'mmol/L');
        }

        return $this->getPrintStr('', $print);
    }


}